package com.zjitc.controller;

import com.zjitc.domain.Stu;
import com.zjitc.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;


    @RequestMapping("/list")
    public String getAll(Model model) {
        List<Stu> stus= studentService.selectAll();
        model.addAttribute("stus", stus);
        return "list";
    }

    @RequestMapping("/update")
    public String updateStu(Stu stu, Model model) {
        System.out.println(stu);
        studentService.updateStu(stu);
        return "redirect:list";
    }

    @RequestMapping("/detail")
    public String detail(int id, Model model) {
        Stu getStuById = studentService.getStudentById(id);
        model.addAttribute("student", getStuById);
        return "detail";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        studentService.delete(id);
        return "redirect:/list";
    }
    @RequestMapping("/insert")
    public String insert(){
        return "add";
    }
    @RequestMapping("/insertUser")
    public String insertUser(Stu stu){
        studentService.insert(stu);
        return "redirect:list";
    }
    @RequestMapping("/findByCondition")
    public String findByCondition(Stu stu, Model model) {
        model.addAttribute("stu",stu);
        model.addAttribute("stus",studentService.findByCondition(stu));
        System.out.println(stu);
        return "list";
    }
}
